#!/bin/bash
# vim: dict=/usr/share/beakerlib/dictionary.vim cpt=.,w,b,u,t,i,k
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
#   runtest.sh of /CoreOS/systemtap/Sanity/userspace-probes
#   Description: userspace-probes
#   Author: Martin Cermak <mcermak@redhat.com>
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
#   Copyright (c) 2013 Red Hat, Inc. All rights reserved.
#
#   This copyrighted material is made available to anyone wishing
#   to use, modify, copy, or redistribute it subject to the terms
#   and conditions of the GNU General Public License version 2.
#
#   This program is distributed in the hope that it will be
#   useful, but WITHOUT ANY WARRANTY; without even the implied
#   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
#   PURPOSE. See the GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public
#   License along with this program; if not, write to the Free
#   Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
#   Boston, MA 02110-1301, USA.
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Include Beaker environment
. /usr/share/beakerlib/beakerlib.sh || exit 1

export DEBUGINFOD_URLS="https://debuginfod.fedoraproject.org/ $DEBUGINFOD_URLS"
export DEBUGINFOD_TIMEOUT=300

PACKAGE="$(rpm -qf $( which stap ) | head -n 1 )"
WORKDIR=$( mktemp -d )
TESTUSER=mytestuser



rlJournalStart
    rlPhaseStart WARN "Check the environment"
        if ! stap -V 2>&1 | grep -q 'enabled features:.*DYNINST'; then
            rlLogWarning "No dyninst available in $PACKAGE"
            rlPhaseEnd
            rlJournalPrintText
            rlJournalEnd
            exit 0
        fi
        rlRun "useradd $TESTUSER" 0,9
        rlRun "su $TESTUSER -c 'which stap'"
        rlRun "rpm -qa | fgrep -e dyninst -e systemtap | sort"
    rlPhaseEnd

    rlPhaseStartSetup
        rlRun "cp hello.c testcase*.sh $WORKDIR"
        rlRun "chown -R $TESTUSER:$TESTUSER $WORKDIR"
    rlPhaseEnd

    rlPhaseStart FAIL "Testcase 1"
        rlRun "which gcc"
        rlRun "su $TESTUSER -c 'gcc -g -o $WORKDIR/hello $WORKDIR/hello.c'"
        rlRun "su $TESTUSER -c '$WORKDIR/hello'"
        rlRun "chmod a+x $WORKDIR/testcase1.sh"
        rlRun "su $TESTUSER -c '$WORKDIR/testcase1.sh $WORKDIR'"

        LOG="$WORKDIR/stapout.log"

        rlRun "cat $LOG"
        rlRun "grep '^process(\"$WORKDIR/hello\").function(\"main@$WORKDIR/hello.c:3\")$' $LOG"
        rlRun "grep -i error $LOG" 1
    rlPhaseEnd

    rlPhaseStart FAIL "Testcase 2"
        rlRun "chmod a+x $WORKDIR/testcase2.sh"
        rlRun "su $TESTUSER -c '$WORKDIR/testcase2.sh $WORKDIR'"

        LOG="$WORKDIR/stapout.log"

        rlRun "cat $LOG"
        rlRun "grep '^\*\*\*\ exiting\ \*\*\*$' $LOG"
        rlRun "egrep -i 'error|warning' $LOG" 1
    rlPhaseEnd

    rlPhaseStartCleanup
        rlRun "userdel $TESTUSER" 0-255
        rlRun "rm -rf /home/$TESTUSER $WORKDIR"
    rlPhaseEnd
rlJournalPrintText
rlJournalEnd
